﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace IronCow.UnitTests.Mock
{
    public class MockTaskResponsePart : MockResponsePart
    {
		public string TaskId { get; set; }
        public DateTime Added { get; set; }
        public DateTime? Due { get; set; }
        public bool HasDueTime { get; set; }
        public DateTime? Completed { get; set; }
        public DateTime? Deleted { get; set; }
        public TaskPriority Priority { get; set; }
        public int Postponed { get; set; }
        public string Estimate { get; set; }

		public MockTaskResponsePart(int taskId)
            : this(taskId, DateTime.Today)
        {
        }

		public MockTaskResponsePart(int taskId, DateTime added)
            : this(taskId, added, TaskPriority.None)
        {
        }

		public MockTaskResponsePart(int taskId, DateTime added, TaskPriority priority)
            : this(taskId, added, priority, null)
        {
        }

		public MockTaskResponsePart(int taskId, DateTime added, TaskPriority priority, DateTime? due)
        {
            TaskId = taskId.ToString();
            Added = added;
            Priority = priority;
            Due = due;
        }

        public override void GetResponseMarkup(System.Xml.XmlWriter markupWriter, Dictionary<string, string> parameters)
        {
            markupWriter.WriteStartElement("task");
            markupWriter.WriteAttributeString("id", TaskId.ToString());

            if (parameters["method"] == "rtm.tasks.setDueDate")
                markupWriter.WriteAttributeString("due", parameters["due"]);
            else if (Due.HasValue)
                markupWriter.WriteAttributeString("due", Due.Value.ToString("s"));

            markupWriter.WriteAttributeString("has_due_time", HasDueTime ? "1" : "0");
            markupWriter.WriteAttributeString("added", Added.ToString("s"));
            if (Completed.HasValue) markupWriter.WriteAttributeString("completed", Completed.Value.ToString("s"));
            if (Deleted.HasValue) markupWriter.WriteAttributeString("deleted", Deleted.Value.ToString("s"));
            switch (Priority)
            {
                case TaskPriority.None:
                    markupWriter.WriteAttributeString("priority", "N");
                    break;
                case TaskPriority.One:
                    markupWriter.WriteAttributeString("priority", "1");
                    break;
                case TaskPriority.Two:
                    markupWriter.WriteAttributeString("priority", "2");
                    break;
                case TaskPriority.Three:
                    markupWriter.WriteAttributeString("priority", "3");
                    break;
            }
            markupWriter.WriteAttributeString("postponed", Postponed.ToString());
            markupWriter.WriteAttributeString("estimate", Estimate);
            markupWriter.WriteEndElement();
        }
    }
}
